package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.KnowledgeModel;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangpan on 2018/5/19.
 */

public class DataQueryListAdapter extends RecyclerView.Adapter<DataQueryListAdapter.ViewHolder> {

    private Context context;
    private List<DataHolder> list;
    private final LayoutInflater inflater;
    private static final int TITLE = 0;
    private static final int CONTENT = 1;
    private static final int MORE = 2;
    private String key, flag;
    private int f;

    public DataQueryListAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void setKey(String tag, String key) {
        this.flag = tag;//标志
        this.key = key;//搜索内容
        switch (tag) {
            case "repository":
                f = 1;
                break;
            case "case":
                f = 2;
                break;
        }
    }

    //筹划案例
    public void setTax(List<KnowledgeModel.DataBean> caseTax) {
        if (caseTax != null && caseTax.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("筹划案例");
            this.list.add(titleData);
            for (int i = 0; i < (caseTax.size() > 5 ? 5 : caseTax.size()); i++) {
                ContentData data = new ContentData();
                data.setData(caseTax.get(i));
                this.list.add(data);
            }
            if (caseTax.size() > 5) {
                MoreData moreData = new MoreData();
                moreData.setData("tax");
                this.list.add(moreData);
            }
        }
        notifyDataSetChanged();
    }

    //法院判例
    public void setJudicial(List<KnowledgeModel.DataBean> caseCourt) {
        if (caseCourt != null && caseCourt.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("法院判例");
            this.list.add(titleData);

            for (int i = 0; i < (caseCourt.size() > 5 ? 5 : caseCourt.size()); i++) {
                ContentData data = new ContentData();
                data.setData(caseCourt.get(i));
                this.list.add(data);
            }
            if (caseCourt.size() > 5) {
                MoreData moreData = new MoreData();
                moreData.setData("judicial");
                this.list.add(moreData);
            }
        }
        notifyDataSetChanged();
    }

    //稽查案例
    public void setAuditing(List<KnowledgeModel.DataBean> caseCheck) {
        if (caseCheck != null && caseCheck.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("稽查案例");
            this.list.add(titleData);

            for (int i = 0; i < (caseCheck.size() > 5 ? 5 : caseCheck.size()); i++) {
                ContentData data = new ContentData();
                data.setData(caseCheck.get(i));
                this.list.add(data);
            }
            if (caseCheck.size() > 5) {
                MoreData moreData = new MoreData();
                moreData.setData("auditing");
                this.list.add(moreData);
            }
        }
        notifyDataSetChanged();
    }

    //名师导航
    public void setNavigation(List<KnowledgeModel.DataBean> knowledgeTeacher) {
        if (knowledgeTeacher != null && knowledgeTeacher.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("名师导航");
            this.list.add(titleData);

            for (int i = 0; i < (knowledgeTeacher.size() > 5 ? 5 : knowledgeTeacher.size()); i++) {
                ContentData data = new ContentData();
                data.setData(knowledgeTeacher.get(i));
                this.list.add(data);
            }
            if (knowledgeTeacher.size() > 5) {
                MoreData moreData = new MoreData();
                moreData.setData("navigation");
                this.list.add(moreData);
            }
        }
        notifyDataSetChanged();
    }

    //绝对收藏
    public void setCollection(List<KnowledgeModel.DataBean> knowledgeCollect) {
        if (knowledgeCollect != null && knowledgeCollect.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("绝对收藏");
            this.list.add(titleData);

            for (int i = 0; i < (knowledgeCollect.size() > 5 ? 5 : knowledgeCollect.size()); i++) {
                ContentData data = new ContentData();
                data.setData(knowledgeCollect.get(i));
                this.list.add(data);
            }
            if (knowledgeCollect.size() > 5) {
                MoreData moreData = new MoreData();
                moreData.setData("collection");
                this.list.add(moreData);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public DataQueryListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TITLE:
                View titleView = inflater.inflate(R.layout.search_title_layout, null);
                return new TitleViewHolder(titleView);
            case CONTENT:
                View contentView = inflater.inflate(R.layout.search_content_layout, null);
                return new ContentViewHolder(contentView);
            case MORE:
                View moreView = inflater.inflate(R.layout.search_more_layout, parent, false);
                return new MoreViewHolder(moreView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(DataQueryListAdapter.ViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            holder.setData(list.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    public abstract class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void setData(DataHolder data);
    }

    public abstract class DataHolder<T> {
        private T data;

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        abstract int getType();
    }

    public class TitleData extends DataHolder {

        @Override
        int getType() {
            return TITLE;
        }
    }

    public class ContentData extends DataHolder {

        @Override
        int getType() {
            return CONTENT;
        }
    }

    public class MoreData extends DataHolder {

        @Override
        int getType() {
            return MORE;
        }
    }

    public class TitleViewHolder extends ViewHolder {

        TextView title_tv;

        public TitleViewHolder(View itemView) {
            super(itemView);
            title_tv = itemView.findViewById(R.id.title);
        }

        @Override
        public void setData(DataHolder data) {
            String name = (String) data.getData();
            title_tv.getPaint().setFakeBoldText(true);
            title_tv.setText(name);
        }
    }

    public class ContentViewHolder extends ViewHolder {
        TextView content_tv;

        public ContentViewHolder(View itemView) {
            super(itemView);
            content_tv = itemView.findViewById(R.id.content);
        }

        @Override
        public void setData(DataHolder data) {
            final KnowledgeModel.DataBean s = (KnowledgeModel.DataBean) data.getData();
            content_tv.setText(s.getTitle());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goKnowledgeDetailsAcitivty(context, s.getUuid(), f);
                }
            });
        }
    }

    public class MoreViewHolder extends ViewHolder {

        TextView more_tv;

        public MoreViewHolder(View itemView) {
            super(itemView);
            more_tv = itemView.findViewById(R.id.more);
            more_tv.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        }

        @Override
        public void setData(DataHolder data) {
            final String tag = (String) data.getData();
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (tag) {
                        case "tax":
                            IntentManager.goMoreDataQueryActivity(context, key, "筹划案例", flag, "0");
                            break;
                        case "judicial"://
                            IntentManager.goMoreDataQueryActivity(context, key, "法院判例", flag, "1");
                            break;
                        case "auditing"://稽查
                            IntentManager.goMoreDataQueryActivity(context, key, "稽查案例", flag, "2");
                            break;
                        case "navigation"://名师
                            IntentManager.goMoreDataQueryActivity(context, key, "名师导航", flag, "0");
                            break;
                        case "collection"://收藏
                            IntentManager.goMoreDataQueryActivity(context, key, "绝对收藏", flag, "1");
                            break;
                    }
                }
            });
        }
    }

    public void clearAdapter() {
        if (list != null) {
            list.clear();
            notifyDataSetChanged();
        }
    }


}
